package com.huarui.task.service.impl;

import java.util.Date;
import java.util.List;

import org.ewaf.framework.model.PagingInfo;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.huarui.task.dao.TaskDao;
import com.huarui.task.dao.TaskDetailDao;
import com.huarui.task.dao.TaskOwnerDao;
import com.huarui.task.model.Task;
import com.huarui.task.model.TaskDetail;
import com.huarui.task.model.TaskListInfo;
import com.huarui.task.model.TaskOwner;
import com.huarui.task.service.TaskService;
import com.huarui.task.service.provider.CreateTaskOwnerHandler;
import com.huarui.task.service.provider.TaskDetailProvider;
import com.huarui.task.service.provider.TaskOwnerProvider;
import com.huarui.task.service.provider.TaskProvider;
import com.huarui.task.service.provider.UpdateTaskOwnerHandler;
import com.huarui.task.service.provider.WorkflowInfoProvider;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;

public class TaskServiceImpl implements TaskService {


	private TaskDao taskDao;
	public TaskDetailDao taskDetailDao;

	private TaskOwnerDao taskOwnerDao;

	public TaskOwnerDao getTaskOwnerDao() {
		return taskOwnerDao;
	}

	public void setTaskOwnerDao(TaskOwnerDao taskOwnerDao) {
		this.taskOwnerDao = taskOwnerDao;
	}

	private TransactionTemplate transactionTemplate;

	public TransactionTemplate getTransactionTemplate() {
		return transactionTemplate;
	}

	public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
		this.transactionTemplate = transactionTemplate;
	}

	public TaskDao getTaskDao() {
		return taskDao;
	}

	public void setTaskDao(TaskDao taskDao) {
		this.taskDao = taskDao;
	}

	public TaskDetailDao getTaskDetailDao() {
		return taskDetailDao;
	}

	public void setTaskDetailDao(TaskDetailDao taskDetailDao) {
		this.taskDetailDao = taskDetailDao;
	}

	public void getPagingNewTask(PagingInfo<TaskListInfo, TaskListInfo> paginInfo) {
		TaskListInfo condition = paginInfo.getCondition();
		TaskOwner owner=new TaskOwner();
		owner.setOwnerId(UserHolder.getCurrentSessionMessage().getCompanyId());
		if (condition == null) {
			condition = new TaskListInfo();
			condition.setOwner(owner);
		} else if (condition.getOwner() == null) {
			condition.setOwner(owner);
		}
		condition.getOwner().setOwnerId(UserHolder.getCurrentSessionMessage().getCompanyId().intValue());
		List<TaskListInfo> items = this.taskDetailDao.findPagingNewTasks(condition, paginInfo.getSummary());
		int total = this.taskDetailDao.findNewTaskCount(condition);
		// logger.info("查询结果=="+items);
		paginInfo.setItems(items);
		paginInfo.setTotal(total);
	}

	private Task createNewTask(Integer workFlowId, String tableName, Integer tableId, Integer status, Integer statusId, String statusName) {

		Task task = new Task();

		SessionMessage sm = UserHolder.getCurrentSessionMessage();

		task.setTableId(tableId);
		task.setTableName(tableName);

		task.setStatus(status);
		task.setStatusId(statusId);
		task.setStatusName(statusName);

		task.setCreateCompanyName(sm.getCompanyName());
		task.setCreateCompanyId(sm.getCompanyId());
		task.setCreatorId(sm.getUserId());
		task.setCreatorName(sm.getUserName());

		task.setCurrentCompanyId(sm.getCompanyId());
		task.setCurrentCompanyName(sm.getCompanyName());
		task.setCurrentorId(sm.getUserId());
		task.setCurrentorName(sm.getUserName());

		task.setPreActorId(0);
		task.setPreActionCompanyId(0);
		task.setPreActorName("");
		task.setPreActionCompanyName("");
		task.setPreStatus(0);
		task.setPreStatusId(0);
		task.setPreStatusName("");

		task.setFinished(false);
		task.setGmtEnd(null);
		task.setGmtModify(new Date());
		task.setGmtStart(new Date());

		task.setPartentId(0);

		task.setRevoked(true);

		task.setSubTaskNum(0);

		task.setWorkFlowId(workFlowId);
		return task;
	}

	public boolean addNewTask(Integer workFlowId, String tableName, Integer tableId, String tableCode ,Integer status, Integer statusId, String statusName, String remark, String taskName) {

		final Task task = createNewTask(workFlowId, tableName, tableId, status, statusId, statusName);

		final TaskDetail taskDetail = createNewTaskDetail(task.getId(), status, statusId, statusName, remark);

		final TaskOwner taskOwner = createNewTaskOwner(task.getId(), tableCode, remark, taskName);

		transactionTemplate.execute(new TransactionCallback() {

			@Override
			public Object doInTransaction(TransactionStatus status) {

				taskDao.create(task);
				taskDetail.setTaskId(task.getId());
				taskDetailDao.create(taskDetail);
				taskOwner.setTaskId(task.getId());
				taskOwnerDao.create(taskOwner);

				return null;
			}
		});

		return true;
	}
	
	private WorkflowInfoProvider  workflowInfoProvider;
	

	public WorkflowInfoProvider getWorkflowInfoProvider() {
		return workflowInfoProvider;
	}

	public void setWorkflowInfoProvider(WorkflowInfoProvider workflowInfoProvider) {
		this.workflowInfoProvider = workflowInfoProvider;
	}

	private TaskOwner createNewTaskOwner(Integer taskId, String  tableCode , String remark, String taskName) {
		TaskOwner taskOwner = new TaskOwner();
		SessionMessage sm = UserHolder.getCurrentSessionMessage();
		taskOwner.setOwnerId(sm.getUserId());
		// taskOwner.setTableId(tableId);
		taskOwner.setTableCode(tableCode);
		taskOwner.setTaskId(taskId);
		taskOwner.setTaskName(taskName);
		taskOwner.setRemark(remark);
		return taskOwner;
	}

	public TaskDetail createNewTaskDetail(Integer taskId, Integer status, Integer statusId, String statusName, String remark) {
		TaskDetail taskDetail = new TaskDetail();

		taskDetail.setTaskId(taskId);

		SessionMessage sm = UserHolder.getCurrentSessionMessage();

		taskDetail.setActionCompanyId(sm.getCompanyId());
		taskDetail.setActionCompanyName(sm.getCompanyName());
		taskDetail.setActorId(sm.getUserId());
		taskDetail.setActorName(sm.getUserName());

		taskDetail.setGmtAction(new Date());

		taskDetail.setPreviouseId(0);

		taskDetail.setRemark(remark);

		taskDetail.setSequence(0);

		taskDetail.setInitStatus(status);
		taskDetail.setInitStatusId(statusId);
		taskDetail.setInitStatusName(statusName);

		taskDetail.setEndStatus(0);
		taskDetail.setEndStatusId(0);
		taskDetail.setEndStatusName("");

		return taskDetail;
	}

	@Override
	public boolean addNewTask(
			TaskProvider taskProvider, TaskDetailProvider taskDetailProvider,
			TaskOwnerProvider taskOwnerProvider) {
		final Task task = taskProvider.createTask();

		final TaskDetail taskDetail = taskDetailProvider.createTaskDetail();

		final Object obj_taskOwner = ((CreateTaskOwnerHandler)taskOwnerProvider).createTaskOwner();

		transactionTemplate.execute(new TransactionCallback() {

			@SuppressWarnings("unchecked")
			@Override
			public Object doInTransaction(TransactionStatus status) {

				taskDao.create(task);
				taskDetail.setTaskId(task.getId());
				taskDetailDao.create(taskDetail);
				
				if(obj_taskOwner instanceof List) {
					List<TaskOwner> taskOwnerList = (List<TaskOwner>) obj_taskOwner;
					for(TaskOwner taskOwner : taskOwnerList) {
						taskOwner.setTaskId(task.getId());
						taskOwnerDao.create(taskOwner);
					}
				} else {
					TaskOwner taskOwner = (TaskOwner) obj_taskOwner;
					if(taskOwner != null) {
						taskOwner.setTaskId(task.getId());
						taskOwnerDao.create(taskOwner);
					}
				}

				return null;
			}
		});

		return true;
	}

	@Override
	public boolean update(final TaskProvider taskProvider,
			final TaskDetailProvider taskDetailProvider,
			final TaskOwnerProvider taskOwnerProvider) {

		transactionTemplate.execute(new TransactionCallback() {
			
			@SuppressWarnings("unchecked")
			@Override
			public Object doInTransaction(TransactionStatus status) {
				Task task = taskProvider.updateTask();
				taskDao.update(task);
				
				TaskDetail  taskDetail = taskDetailProvider.createTaskDetail();
				taskDetail.setTaskId(task.getId());
				taskDetailDao.create(taskDetail);
				
				
				if( taskOwnerProvider instanceof  CreateTaskOwnerHandler ) {
					Object obj_taskOwner = ((CreateTaskOwnerHandler)taskOwnerProvider).createTaskOwner();
					if(obj_taskOwner instanceof List) {
						List<TaskOwner> taskOwnerList = (List<TaskOwner>) obj_taskOwner;
						for(TaskOwner taskOwner : taskOwnerList) {
							taskOwner.setTaskId(task.getId());
							taskOwnerDao.create(taskOwner);
						}
					} else {
						TaskOwner taskOwner = (TaskOwner) obj_taskOwner;
						if(taskOwner != null) {
							taskOwner.setTaskId(task.getId());
							taskOwnerDao.create(taskOwner);
						}
					}
					
				}
				
				if ( taskOwnerProvider instanceof UpdateTaskOwnerHandler ) {
					Object obj_taskOwner = ((UpdateTaskOwnerHandler)taskOwnerProvider).updateTaskOwner();
					if(obj_taskOwner instanceof TaskOwner) {
						TaskOwner taskOwner = (TaskOwner) obj_taskOwner;
						if(taskOwner != null) {
							taskOwner.setTaskId(task.getId());
							taskOwnerDao.update(taskOwner);
						}
					} else {
						List<TaskOwner> list = (List<TaskOwner>) obj_taskOwner;
						for(TaskOwner taskOwner : list) {
							taskOwner.setTaskId(task.getId());
							taskOwnerDao.update(taskOwner);
						}
					}
				}
				
				
				return null;
			}
		});
		
		return false;
	}
}
